package com.study.sbsummary.config.constant;

import org.apache.commons.lang3.StringUtils;

import java.util.LinkedHashSet;
import java.util.Set;

public enum Platform {
    IOS(0, "ios"),
    ANDROID(1, "android");

    private int code;
    private String name;

    private Platform(int pf, String name) {
        this.code = pf;
        this.name = name;
    }

    public int code() {
        return code;
    }

    public String getName() {
        return name;
    }

    public static Platform byName(String name) {
        for (Platform platform : values()) {
            if (platform.getName().equals(name)) {
                return platform;
            }
        }
        return null;
    }

    public static Platform byCode(int code) {
        return byCode(Integer.valueOf(code));
    }

    public static Platform byCode(Integer code) {
        if (code == null) {
            return null;
        }
        for (Platform platform : values()) {
            if (code == platform.code()) {
                return platform;
            }
        }
        return null;
    }

    /**
     * 场景一  use_platform = 0
     * 场景二  use_platform = 1
     * 场景三  use_platform = 0,1
     * 场景四  use_platform = 1,0
     * 场景五  use_platform = 0, 1
     * 场景六  use_platform = 1, 0
     */
    public static String transferCodeToPlatformName(String usePlatform) {
        if (StringUtils.isBlank(usePlatform)) {
            throw new IllegalArgumentException("不合法的use_platform参数");
        }

        int length = usePlatform.trim().length();
        if (length == 1) {
            int code = Integer.parseInt(usePlatform);
            Platform platform = byCode(code);
            if (platform == null) {
                throw new IllegalArgumentException("不能识别的use_platform");
            }
            return platform.name;
        }

        StringBuilder stringBuilder = new StringBuilder();
        Set<Platform> platforms = new LinkedHashSet<>();
        if (length == 3 || length == 4) {
            int index = usePlatform.indexOf(",");
            if (index == -1) {
                throw new IllegalArgumentException("不合法的use_platform参数");
            }
            String[] codes = usePlatform.split(",");
            if (codes.length == 0) {
                throw new IllegalArgumentException("不合法的use_platform参数");
            }
            for (String code : codes) {
                if (StringUtils.isBlank(code)) {
                    throw new IllegalArgumentException("不合法的use_platform参数");
                }
                String codeTrimValue = code.trim();
                int codeIntValue = Integer.parseInt(codeTrimValue);
                Platform platform = byCode(codeIntValue);
                if (platform == null) {
                    throw new IllegalArgumentException("不能识别的use_platform");
                }
                platforms.add(platform);
            }
        }

        if (length > 4) {
            throw new IllegalArgumentException("不合法的use_platform参数");
        }

        for (Platform p : platforms) {
            stringBuilder.append(p.name);
            stringBuilder.append(",");
        }
        String result = stringBuilder.toString();
        if (result.length() > 1) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }
}
